﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BOMS.Model;
using BOMS.DBUtility;

namespace BOMS.DAL
{
    public class BatteryAnaly
    {
        
        /// <summary>
        /// 取电池或电池组的历史记录数据。
        /// 抽样（间隔>3秒）或实际（间隔=3秒）
        /// </summary>
        /// <param name="key">组合主键</param>
        /// <param name="param"></param>
        /// <param name="PageCount"></param>
        /// <returns>
        /// key.BatteryID=-1时，返回电池组历史表：Vval,Ival,CapFree,LogID,Dtm,ID
        /// key.BatteryID!=-1时，返回电池历史表：Vval,Rval,Tval,LogID,Dtm,ID
        /// </returns>
        public DataSet GetBatteryAbstractData(BatteryKey key,
            AnalyParam param, out int PageCount)
        {
            SqlParameter[] parms = new SqlParameter[10];
            (parms[0] = new SqlParameter("@Generation", SqlDbType.TinyInt)).Value = key.Generation;
            (parms[1] = new SqlParameter("@EquipID", SqlDbType.NVarChar, 30)).Value = key.EquipID;
            (parms[2] = new SqlParameter("@BatteryGrp", SqlDbType.NVarChar,20)).Value = key.BatteryGrp;
            //@BatteryID
            (parms[3] = new SqlParameter("@dtmBegin", SqlDbType.DateTime)).Value = param.dtmBegin;
            (parms[4] = new SqlParameter("@dtmEnd", SqlDbType.DateTime)).Value = param.dtmEnd;
            (parms[5] = new SqlParameter("@interv", SqlDbType.Int)).Value = param.interv;
            (parms[6] = new SqlParameter("@dispCount", SqlDbType.Int)).Value =param.pageSize;
            (parms[7] = new SqlParameter("@curPage", SqlDbType.Int)).Value =param.curPage;
            (parms[8] = new SqlParameter("@pageCount", SqlDbType.Int)).Direction = ParameterDirection.Output;

            string ProcName = "";
            //电池
             if(key.BatteryID != -1)
            {
                ProcName = "dbo.sp_BuildBatteryAbstractData";
                (parms[9] = new SqlParameter("@BatteryID", SqlDbType.Int)).Value = key.BatteryID;//电池编号
            }
            //电池组
            else
            {
                ProcName = "dbo.sp_BuildBatteryGrpAbstractData";
               
            }
            
            DataSet ds = DbHelperSQL.RunProcedure(ProcName,parms, "FPGOUT");
            PageCount = (int)parms[8].Value;
            return ds;
        }
        public void AssignModel(DataRow dr,Model. BatteryAnaly model)
        {
            if (dr["Vval"] != DBNull.Value && dr["Vval"].ToString() != "")
            {
                model.Vval = (Decimal)dr["Vval"];
            }
            if (dr.Table.Columns.Contains("Ival") && dr["Ival"] != DBNull.Value && dr["Ival"].ToString() != "")
            {
                model.Ival = (Decimal)dr["Ival"];
            }
            if (dr.Table.Columns.Contains("Rval") && dr["Rval"] != DBNull.Value && dr["Rval"].ToString() != "")
            {
                model.Rval = (Decimal)dr["Rval"];
            }
            if (dr.Table.Columns.Contains("Tval") && dr["Tval"] != DBNull.Value && dr["Tval"].ToString() != "")
            {
                model.Tval = (Decimal)dr["Tval"];
            }
            if (dr.Table.Columns.Contains("CapFree") && dr["CapFree"] != DBNull.Value && dr["CapFree"].ToString() != "")
            {
                model.CapFree = (Decimal)dr["CapFree"];
            }
            if (dr["Dtm"] != DBNull.Value)
            {
                model.Time = (DateTime)dr["Dtm"];
            }
            if (dr["LogID"] != DBNull.Value)
            {
                model.LogID = (long)dr["LogID"];
            }
        }
    }
}
